package com.gem.service.impl;

import com.gem.domain.JobAPI;
import com.gem.domain.Student;
import com.gem.domain.StudentAPI;
import com.gem.mapper.CareerMapper;
import com.gem.mapper.ScoreMapper;
import com.gem.mapper.StudentMapper;
import com.gem.service.StudentService;
import com.gem.util.ConditionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * @author Apolar_GuanYH-管阳红
 * Description:
 * create 2019/11/5 21:57
 * Modified By:
 */
@Service
public class StudentServiceImpl implements StudentService {

    private StudentMapper stuMapper;

    @Autowired
    public void setStuMapper(StudentMapper stuMapper) {
        this.stuMapper = stuMapper;
    }

    private CareerMapper careerMapper;

    @Autowired
    public void setCareerMapper(CareerMapper careerMapper) {
        this.careerMapper = careerMapper;
    }

    private ScoreMapper scoreMapper;

    @Autowired
    public void setScoreMapper(ScoreMapper scoreMapper) {
        this.scoreMapper = scoreMapper;
    }

    //按班级表ID查询所有学生
    @Override
    public List<Student> selectAllStuByClassService(Integer classId) {
        return stuMapper.selectAllStuByClass(classId);
    }

    //按学生ID查询学生
    @Override
    public List<Student> selectStuBySidService(Integer sId) {
        return stuMapper.selectStuBySid(sId);
    }

    //增加学生
    @Override
    @Transactional
    public boolean addStuService(List<StudentAPI> stuAPIList) {
        boolean isSuccess = true;
        for (StudentAPI stuAPI : stuAPIList) {
            if (stuAPI.getS_sex() == null) {
                stuAPI.setS_sex(0);
            }
            JobAPI jobAPI = new JobAPI();
            jobAPI.setSid(stuAPI.getId());
            jobAPI.setCareerClassId(stuAPI.getC_id());
            jobAPI.setCareerStuName(stuAPI.getS_name());
            boolean flag = false;
            if (stuMapper.addStu(stuAPI)) {
                if (scoreMapper.add(stuAPI.getId())) {
                    if (careerMapper.insertJobAPI(jobAPI)) {
                        flag = true;
                    }
                }
            }
            if (!flag) {
                isSuccess = false;
                break;
            }
        }
        if (!isSuccess) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return isSuccess;
    }

    //删除（伪删）学生
    @Override
    public boolean deleteStuService(Integer sId) {
        return stuMapper.deleteStu(sId);
    }

    //修改学生
    @Override
    public boolean updateStuService(StudentAPI stuAPI) {

        if (stuAPI.getS_sex() == null) {
            stuAPI.setS_sex(0);
        }
        return stuMapper.updateStu(stuAPI);
    }

    //按条件查询
    @Override
    public List<Student> selectStuByCondition(ConditionUtil cu) {
        return stuMapper.selectStuByCondition(cu);
    }
}
